package com.updesignsz.admin.mapper;

import com.updesignsz.admin.vo.form.FunctionForm;
import com.updesignsz.commom.entity.Function;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * TODO 功能
 * Created User: lx
 * Date: 2020/4/28 15:05
 */
@Repository
@Mapper
public interface FunctionMapper {

    @Select("select identifier from t_function where id in (${functionIds})")
    List<String> findByFunctionIdList(@Param("functionIds")String functionIds);


    @Select("select module_id from t_function where id in (${functionIds}) GROUP BY module_id")
    List<String> getFunctionModuleList(@Param("functionIds") String functionIds);

    @Select("select * from t_function where id in (select function_id from t_role_function where role_id = #{roleId})")
    @Results({
            @Result(property = "module", column = "module_id", many = @Many(select = "com.updesignsz.admin.mapper.ModuleMapper.findModuleByFunctionId"))
    })
    List<Function> findFunctionByRoleId(@Param("roleId") Integer roleId);


    @Select("select * from t_function order by created_date desc")
    @Results({
            @Result(property = "module", column = "module_id", one = @One(select = "com.updesignsz.admin.mapper.ModuleMapper.findModuleByFunctionId"))
    })
    List<Function> listAll();


    @Select("select * from t_function where id = #{id}")
    @Results({
            @Result(property = "module", column = "module_id", one = @One(select = "com.updesignsz.admin.mapper.ModuleMapper.findModuleByFunctionId"))
    })
    Function getById(@Param("id") Integer id);

    @Insert({
            "insert into t_function(name, identifier, module_id, remark, created_date, updated_date, is_removed) ",
            "values (#{name},#{identifier},#{moduleId},#{remark},NOW(),NOW(),0)"
    })
    @Options(useGeneratedKeys = true, keyColumn = "id")
    void saveFunction(FunctionForm function);

    @Update({"<script>",
            "update t_function <set>",
            "<if test=\"identifier != null\" >identifier = #{identifier},</if>",
            "<if test=\"name != null\" >name = #{name},</if>",
            "<if test=\"moduleId != null\" >module_id = #{moduleId},</if>",
            "<if test=\"remark != null\" >remark = #{remark},</if>",
            "updated_date = NOW()",
            "</set> where id = #{id}",
            "</script>"
    })
    void updateFunction(FunctionForm functionForm);

    @Delete("delete from t_function where id = #{id}")
    void deleteFunction(@Param("id") Integer id);

}
